package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.RowBase;

@Controller
@RequestMapping("/DeclareController/*")
public class DeclareController extends BaseController{
	protected Logger logger = LoggerFactory.getLogger(getClass());
	/**
	 * 获取事件数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getEventData")
	public void getEventData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String wwtp_id=RequestUnit.getParameter("WWTP_ID");
		String st=RequestUnit.getParameter("START_TIME");
		String et=RequestUnit.getParameter("END_TIME");
		String sql="select t3.name,t1.* from OP_ALAR_PROC t1\n" +
						"        left join OP_ALAR_INFO t2 on t1.alarm_id=t2.guid\n" + 
						"        left join BC_SITE_INFO t3 on t2.station_code=t3.exp_no\n" + 
						"        where  (((t1.EXCEPTION_REASON='02' or t1.EXCEPTION_REASON='03' or t1.EXCEPTION_REASON='06' or t1.EXCEPTION_REASON='04')))";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		if (wwtp_id != null && !wwtp_id.equals("")) {
			 sql+=" and t3.wwtp_id =@wwtp_id";
			 parms.add(new GlobalParameter("wwtp_id",wwtp_id) );
		}
		 if (st != null && !st.equals("")) {
				sql = sql + " and t1.CREATETIME >=to_date('"+st+"','yyyy-MM-dd')";
			}
		    if (et != null && !et.equals("")) {
				sql = sql +" and t1.CREATETIME <=to_date('"+et+"','yyyy-MM-dd')";
			}
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		String result = DataUtil.getPageSelectJson(sql, parms,pTable);
		outString(result);
	}
	/**
	 * 获取申报污水厂运营费用数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getServerData")
	public void getServerData(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String wwtp_id=RequestUnit.getParameter("WWTP_ID");
		String st=RequestUnit.getParameter("START_TIME");
		String et=RequestUnit.getParameter("END_TIME");
		//String sql=" SELECT * FROM EXPENSE_CLAIM where 1=1 ";
		String sql=" SELECT * FROM OPERATION_PRICE where 1=1 ";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		if (wwtp_id != null && !wwtp_id.equals("")) {
			 sql+=" and wwtp_id =@wwtp_id";
			 parms.add(new GlobalParameter("wwtp_id",wwtp_id) );
		}
		if (st != null && !st.equals("")) {
			sql +=" and tm >=to_date('"+st+"','yyyy-MM-dd')";
		}
	    if (et != null && !et.equals("")) {
			sql +=" and tm <=to_date('"+et+"','yyyy-MM-dd')";
		}
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		String result = DataUtil.getPageSelectJson(sql, parms,pTable);
		outString(result);
	}
	/**
	 * 导出申请wod文档
	 * @param resq
	 * @param res
	 */
	@RequestMapping(value="/expDoc")
	public void expDoc(HttpServletRequest resq, HttpServletResponse res){
		String guid=RequestUnit.getParameter("GUID");
		List<GlobalParameter> listParam = new ArrayList<GlobalParameter>();
		Map<String, Object> root = new HashMap<String, Object>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		String sql=" SELECT * FROM OPERATION_PRICE where guid='"+guid+"'";
		try{
			List<RowBase>  list = DataUtil.getPageRow(sql, listParam, pTable, DataUtil.getPageInfo());
			RowBase row = null;
			if (list != null && list.size() > 0) {
				row = list.get(0);
				root.put("sum", row.getSafeString("SUM"));
				root.put("sumc", row.getSafeString("SUMC"));
				root.put("com", row.getSafeString("UNIT"));
				root.put("com", row.getSafeString("UNIT"));
				root.put("date1", row.getSafeString("ST").split(" ")[0].toString()+"至"+row.getSafeString("ET").split(" ")[0].toString());
				root.put("date2", row.getSafeString("ST").split(" ")[0].toString()+"至"+row.getSafeString("ET").split(" ")[0].toString());
				String str=row.getSafeString("TM").toString();
				String y3=str.split("-")[0].toString();
				String m3=str.split("-")[1].toString().split("-")[0].toString();
				String d3=str.split("-")[2].toString().split(" ")[0].toString();
				String str1=row.getSafeString("ST").toString();
				String y1=str1.split("-")[0].toString();
				String m1=str1.split("-")[1].toString().split("-")[0].toString();
				String d1=str1.split("-")[2].toString().split(" ")[0].toString();
				String str2=row.getSafeString("ET").toString();
				String y2=str2.split("-")[0].toString();
				String m2=str2.split("-")[1].toString().split("-")[0].toString();
				String d2=str2.split("-")[2].toString().split(" ")[0].toString();
				root.put("y3", y3);
				root.put("m3", m3);
				root.put("d3", d3);
				root.put("y1", y1);
				root.put("m1", m1);
				root.put("d1", d1);
				root.put("y2", y2);
				root.put("m2", m2);
				root.put("d2", d2);
			}
			outWord(root, "operationCount.xml", "运维费用申请.doc");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		/*String ST=RequestUnit.getParameter("ST");
		String ET=RequestUnit.getParameter("ET");
		String SUM=RequestUnit.getParameter("SUM");
		String SUMC=RequestUnit.getParameter("SUMC");
		String UNIT=RequestUnit.getParameter("UNIT");
		String TM=RequestUnit.getParameter("TM");
		Map<String, Object> root = new HashMap<String, Object>();
		try{
		root.put("date1", ST);
		root.put("date2", ET);
		root.put("sum", SUM);
		root.put("sumc", SUMC);
		root.put("com",UNIT);
		outWord(root, "operationCount.xml", "运维费用申报.doc");
		}
		catch(Exception e){
			e.printStackTrace();
		}*/
		/*String guid=RequestUnit.getParameter("GUID");
		List<GlobalParameter> listParam = new ArrayList<GlobalParameter>();
		Map<String, Object> root = new HashMap<String, Object>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		String sql=" SELECT * FROM EXPENSE_CLAIM where guid='"+guid+"'";
		try{
			List<RowBase>  list = DataUtil.getPageRow(sql, listParam, pTable, DataUtil.getPageInfo());
			RowBase row = null;
			if (list != null && list.size() > 0) {
				row = list.get(0);
				root.put("NAME", row.getSafeString("NAME"));
				root.put("NOTE", row.getSafeString("NOTE"));
			}
			outWord(root, "DeclareSevice.xml", "运维费用申报.doc");
		}
		catch(Exception e){
			e.printStackTrace();
		}*/
		
	}
}
